//
//  FZHomeController.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/26.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit
import YYModel
class FZHomeController: FZVistorController{

    fileprivate lazy var homeViewModel : FZHomeViewModel = FZHomeViewModel()
    
    fileprivate lazy var UpReloadView : UIActivityIndicatorView = {
       
        let upView = UIActivityIndicatorView(activityIndicatorStyle:.gray)
        
        upView.color = UIColor.red
        return upView
    }()
    
    fileprivate lazy var refreshView : FZRefreshControl = {
        let refresh = FZRefreshControl()
        
        refresh.addTarget(self, action: #selector(DownRefresh), for: .valueChanged)
        
       return refresh
    }()
    
    fileprivate lazy var tipLabel : UILabel = {
        let lab = UILabel()
        lab.backgroundColor = UIColor.yellow
        lab.text = "没有加载到最新微博数据"
        lab.textAlignment = .center
        lab.font = UIFont.systemFont(ofSize: 13)
        lab.isHidden = true
        return lab
    }()
    
    func loadTipLabel() {
        
        if let nav  = self.navigationController {
            
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.height + 20 - 35, width: nav.navigationBar.width, height: 35)
            
        }
    }
   
    func tipLabelAnimation(message : String) {
        if tipLabel.isHidden == false {
            return
        }
        tipLabel.text = message
        tipLabel.isHidden = false
        UIView.animate(withDuration: 0.35, animations: { 
            
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: 35)
            
        }) { (_) in
           UIView.animate(withDuration: 0.35, animations: {
            
             self.tipLabel.transform = CGAffineTransform.identity
            
           }, completion: { (_) in
              self.tipLabel.isHidden = true
           })
        }
    }
    func DownRefresh() {
        getNeteworkData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !isLogin {
            
            vistorView?.changeVistorView(imageName: nil, message: nil)
        }else{
            
            getNeteworkData()
            updataTableView()
            loadTipLabel()
        }
    }
   
    func updataTableView() {
        
        tableView.register(FZHomeTabViewCell.self, forCellReuseIdentifier: homeCellId)
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .none
        tableView.tableFooterView = UpReloadView
        tableView.addSubview(refreshView)
    }
    
    func getNeteworkData() {
        
        homeViewModel.loadHomeViewModel(isUpReload : UpReloadView.isAnimating){ (isSuccess,newMessage) in
            
            if !self.UpReloadView.isAnimating
            {
                
               self.tipLabelAnimation(message: newMessage)
            }
            
            if isSuccess{
                
                self.tableView.reloadData()
            }
            self.endReload()
        }
    }
    
    func endReload() {
        
        UpReloadView.stopAnimating()
        
        refreshView.endRefreshing()
    }
}
    
extension FZHomeController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return homeViewModel.statusArr.count 
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCellId, for: indexPath) as! FZHomeTabViewCell
        
        let model = self.homeViewModel.statusArr[indexPath.row]
        
        cell.statusModel = model
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.row == homeViewModel.statusArr.count - 1 && !UpReloadView.isAnimating {
            print("上拉加载")
            
            self.UpReloadView.startAnimating()
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.35, execute: {
                  self.getNeteworkData()
            })
        }
    }
}






















